<?php
ob_start();
if(!isset($_SESSION)){
session_start();
}
if(!isset($_SESSION["userName"])){
	header("location: login.php");
}
?>
<?php require_once("../connection/connect.php");
require_once("user.php");
$user = new user();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>...:: eManazines ::...</title>
     <script type="text/javascript" src="../js/jquery.js"></script>
		
     <style type="text/css">
<!--
@import url("css/style.css");
-->
     </style>
</head>
<body>
    <div class="wrapper">
    <?php include ("top.php");?>
        <div class="body">
<div class="signup-right-wrapper">
                <h3>
                    Quản lý User</h3>
                <div class="signup-right">
                <?php if(!isset($_GET['act'])){?>
                  <table width="760" border="1" align="center" cellpadding="1" cellspacing="0">
                    <tr>
                      <td width="22%">Tên hiển thị</td>
                      <td width="26%">Email</td>
                      <td width="16%">Nhóm</td>
                      <td width="14%">Gói sử dụng</td>
                      <td width="12%">Sửa</td>
                      <td width="10%">Xóa</td>
                    </tr>
                    <?php
					$rs = mysql_query("SELECT * FROM users WHERE group_id != 1");
					while($r = mysql_fetch_array($rs)){
					$group_id = $r['group_id'];
					$package_id = $r['package_id'];
					?>
                    <tr>
                      <td><?php echo $r['username'];?></td>
                      <td><?php echo $r['email'];?></td>
                      <?php $d = mysql_fetch_array(mysql_query("SELECT * FROM groups WHERE group_id = $group_id"));
					  ?>
                      <td><?php echo $d['name'];?></td>
                      <?php $d1 = mysql_fetch_array(mysql_query("SELECT * FROM packages WHERE package_id = $package_id"));?>
                      <td><?php echo $d1['name'];?></td>
                      <td><a href="manager_user.php?act=edit&id=<?php echo $r['id'];?>">
                      <input type="submit" name="edit" id="edit" value="   Sửa   " />
                      </a>
                      </td>
                      <td><a href="manager_user.php?act=delete&id=<?php echo $r['id'];?>">
                        <input type="submit" name="delete" id="delete" value="   Xóa   " />
                      </a></td>
                    </tr>                
          <?php }?>
                  </table>
          <?php }?>        
                  
                  
                  
             <?php if(isset($_GET['act'])&&($_GET['act']=='edit')){
			 $id = $_GET['id'];
			 ?>
             <form name="fedit" id="fedit" action="" method="post">		
             <input type="hidden" name="id" id="id" value="<?php echo $id; ?>"/>		
              <table width="760" border="1" align="center" cellpadding="1" cellspacing="0">
                    <tr>
                      <td width="22%">Tên hiển thị</td>
                      <td width="26%">Email</td>
                      <td width="16%">Nhóm</td>
                      <td width="14%">Gói sử dụng</td>
                    </tr>
                <tr>
                <td>
                <input type="text" name="username" id="username"/>
                </td>
                 <td>
				<input type="text" name="email" id="email"/>	
                </td>                               
                <td>
                 <select name="group_id" id="group_id">
                 <option>Chọn nhóm</option>
				<?php $rs = mysql_query("SELECT * FROM groups ");
				while($row = mysql_fetch_array($rs)){?>
				<option value="<?php echo $row['group_id'];?>"><?php echo $row['name'];?></option>
				<?php } ?>
                </select>
                </td>                
                 <td>
                 <select name="package_id" id="package_id">
                 <option>Chọn gói</option>
				<?php $rs = mysql_query("SELECT * FROM packages");
				while($row = mysql_fetch_array($rs)){?>
				<option value="<?php echo $row['package_id'];?>"><?php echo $row['name'];?></option>
				<?php } ?>
                </select>
                </td>
                </tr>
                <tr><td colspan="4" align="center"><input type="submit" name="edit_ok" value="Đồng ý Sửa"/></td></tr>
            </table>   
            </form>
            <?php
            if(isset($_POST['edit_ok'])){
            $id = $_POST['id'];
            $userName = $_POST['username'];
			$email = $_POST['email'];
			$group_id = $_POST['group_id'];
			$package_id = $_POST['package_id'];	
			/*				
			echo "$id,$userName,$email,$group_id,$package_id";
			exit();		
			*/
			$user->edit($id,$userName,$email,$group_id,$package_id);
			echo "Sửa thành công.";
            } 	
			?>		
			<?php }?>
            
            
            
            <?php if(isset($_GET['act'])&&($_GET['act']=='delete')){
			$id = $_GET['id'];
			?>
            <form action="" method="post">
            <input type="hidden" name="id" id="id" value="<?php echo $id; ?>"/>	
        <table width="760" border="1" align="center" cellpadding="1" cellspacing="0">
    <tr>
                      <td width="22%">Tên hiển thị</td>
                      <td width="26%">Email</td>
                      <td width="16%">Nhóm</td>
                      <td width="14%">Gói sử dụng</td>
                    </tr>
                    <?php $res= mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id = $id"));?>
                    <tr>
                    <td><?php echo $res['username'];?></td>
                    <td><?php echo $res['email'];?></td>
                    <td></td>
                    <td></td>
                    </tr>
				<tr align="center"><td colspan="4">
				<input name="delete_ok" type="submit" id="delete_ok" value="Đồng ý Xóa"/>
                </td></tr>	
           	</table>	
            </form>
            
            <?php
			if(isset($_POST['delete_ok'])){
			$id = $_POST['id'];
			$user->delete($id);
			echo "Xóa thành công.!";
			}
			?>            
			<?php }?>
                  
      </div>
    </div>
            <div class="vide">
            </div>
        </div>
        <?php include("footer.php");?>
    </div>
</body>
</html>
